package com.example.demo.controller;

import com.example.demo.pojo.Article;
import com.example.demo.service.ArticleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;


@RestController
@Api(tags = "信息管理系统")
@RequestMapping("/article")
public class ArticleController  {
    @Autowired
    public ArticleService acrticleService;
    //1
    @GetMapping("/select")
    @ApiOperation("根据ID进行查询")
    @ApiImplicitParam(name = "id",value = "信息编号",required = true)
    public Article select(int id){
        Article article=acrticleService.select(id);
        return article;
    }
    //2
    @PostMapping("/insert")
    @ApiOperation("信息插入")
    public String save(Article article){
       try{
           acrticleService.insert(article);
        return "信息插入成功";
       }catch (RuntimeException e){
           return "数据插入失败"+e.getMessage();
       }
    }
    //3
    @PostMapping("/delete")
    @ApiOperation("信息删除")
    @ApiImplicitParam(name = "id",value = "信息编号",required = true)
    public String delete(int id ){
        acrticleService.delete(id );
        return "信息删除成功";
    }
    //4
    @PostMapping("/update")
    @ApiOperation("信息更新")
    public String update(Article article ){
        acrticleService.update(article );
        return "信息更新成功";
    }
    //5
    @PostMapping("/patchdelete")
    @ApiOperation("信息批量删除")
    @ApiImplicitParam(name = "ids",value = "信息编号，用英文逗号分开",required = true)
    public String patchdelete(int[] ids){
        for (int id: ids){acrticleService.delete(id );}
        return "信息删除成功";
    }
    //6
    @GetMapping("/selectByPage")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="size",value = "页面大小",required = true),
            @ApiImplicitParam(name = "page",value = "页码",required = true)
    })
    public PageInfo seleceByPage(int size, int page){
        PageInfo pageInfo=acrticleService.findByPage(size,page);
        return pageInfo;
    }
    @PostMapping("/findByPage")
    @ApiOperation("具体分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "size",value = "页面大小",required = true),
            @ApiImplicitParam(name = "author",value = "作者"),
            @ApiImplicitParam(name = "title",value = "标题")
    })
    public PageInfo findByPage(int page,int size,String author,String title){
        PageInfo pageInfo=acrticleService.selectByPage(page, size, author, title);
        return pageInfo;
    }
}
